<?php
/**
 *备货规则模型
 *@author shipanpan
 */
class Backup_Model_BackupRule extends Model_Base {

	public function __construct() {
		$this->db = $this->db();
		$this->writeDb = $this->db("write_db");
	}

	/**
	 * 获取一条备货规则
	 * @param  int    $ruleId [description]
	 * @return [type]         [description]
	 */
	public function getOne(int $ruleId) {
		$backupRule = $this->db->fetchRow("select * from b_product_backup_rule where id=?", $ruleId);
		if (!$backupRule) {
			return $this->_error('未找到指定的备货规则！');
		}
		return $backupRule;
	}

	/**
	 * 根据规则名称获取一条备货规则
	 * @param  string    $name 规则名称
	 * @return [type]         [description]
	 */
	public function getByName($name) {
		return $this->db->fetchAll("select * from b_product_backup_rule where name=?", $name);
	}

	/**
	 * 获取一条备货规则详情(包括备货规则的分类规则)
	 * @param int $ruleId 备货规则id
	 * @return 未找到返回false,成功返回array,其中备货规则的分类规则放在键值‘filter_rule’
	 */
	public function getDetail(int $ruleId) {
		$backupRule = $this->getOne($ruleId);
		if (!$backupRule) {
			return false;
		}
		$backupRuleFilterRule = $this->db->fetchAll('select * from b_product_backup_rule_filter_rule where backup_rule_id=?', $backupRule['id']);
		$backupRule['filter_rule'] = $backupRuleFilterRule;
		return $backupRule;
	}

	/**
	 * 保存备货规则
	 * @param  array  $backupRule           备货规则(不包含分类规则部分)
	 * @param  array  $backupRuleFilterRule 备货规则的分类规则
	 * @return [type]                      [description]
	 */
	public function save(array $backupRule, array $backupRuleFilterRule) {
		$writeDb = $this->db('write_db');
		$writeDb->beginTransaction();
		try {
			if (isset($backupRule['id'])) {
				$ruleId = $backupRule['id'];
				$where = $writeDb->quoteInto('id = ?', $ruleId);
				unset($backupRule['id']);
				$writeDb->update('b_product_backup_rule', $backupRule, $where);
				$where = $writeDb->quoteInto('backup_rule_id = ?', $ruleId);
				$writeDb->delete('b_product_backup_rule_filter_rule', $where);
			} else {
				$writeDb->insert('b_product_backup_rule', $backupRule);
				$ruleId = $writeDb->lastInsertId();
			}

			if ($backupRuleFilterRule) {
				foreach ($backupRuleFilterRule as &$oneFilterRule) {
					$oneFilterRule['backup_rule_id'] = $ruleId;
				}

				$this->insertBatch($writeDb, 'b_product_backup_rule_filter_rule', $backupRuleFilterRule);
			}

			$writeDb->commit();
			return $ruleId;
		} catch (Exception $e) {
			$writeDb->rollBack();
			return $this->_error($e->getMessage());
		}
	}

	/**
	 * 启用规则
	 * @param  int    $backupRuleId [description]
	 * @param  [type] $startDate    规则启用开始日期
	 * @param  [type] $endDate      规则启用结束日期
	 * @return [type]               [description]
	 */
	public function turnOn(int $backupRuleId, $startDate, $endDate) {
		$backupRuleId = is_array($backupRuleId) ? $backupRuleId : [$backupRuleId];

		list($startYear, $startMonth, $startDay) = explode('-', $startDate);
		list($endYear, $endMonth, $endDay) = explode('-', $endDate);

		if (!checkdate($startMonth, $startDay, $startYear)) {
			return $this->_error('启用规则的开始时间异常！');
		}
		if (!checkdate($endMonth, $endDay, $endYear)) {
			return $this->_error('启用规则的结束时间异常！');
		}

		if ($endDate <= $startDate) {
			return $this->_error('启用规则的结束时间异常！');
		}

		$writeDb = $this->db('write_db');
		$where = $writeDb->quoteInto('id in(?)', $backupRuleId);
		$modifyUser = Model_User::currentLoginUser()->user_name;
		$set = ['modify_user' => $modifyUser, 'modify_time' => date("Y-m-d H:i:s"), 'is_enable' => 1, 'start_date' => $startDate, 'end_date' => $endDate];

		return $writeDb->update('b_product_backup_rule', $set, $where);
	}

	/**
	 * 禁用规则
	 * @param  int|array   $backupRuleId 要禁用或启用的规则id(支持批量操作)
	 * @return [type]
	 */
	public function turnOff($backupRuleId) {
		$backupRuleId = is_array($backupRuleId) ? $backupRuleId : [$backupRuleId];
		$writeDb = $this->db('write_db');
		$where = $writeDb->quoteInto('id in(?)', $backupRuleId);
		$modifyUser = Model_User::currentLoginUser()->user_name;
		$set = ['modify_user' => $modifyUser, 'modify_time' => date("Y-m-d H:i:s"), 'is_enable' => 2];
		return $writeDb->update('b_product_backup_rule', $set, $where);
	}
}